package com.cdgeekcamp.redas.lib.core.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.sql.Timestamp;

public class RedasString {
    public static String bytesToHex(byte[] hash) {
        StringBuilder hexString = new StringBuilder();

        for (byte b : hash) {
            String hex = Integer.toHexString(0xff & b);

            if (hex.length() == 1)
                hexString.append('0');

            hexString.append(hex);
        }

        return hexString.toString();
    }

    public static String getPlatform(String url){
        /**
         * 从url中提取出平台
         *
         * @return String name
         */
        String pattern = "(?<=://)[a-zA-Z\\.0-9]+(?=\\/)";
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(url);
        if (m.find( )) {
            String host = m.group(0);
            return HostNameMap.getName(host);
        } else {
            return "错误url，无法提取平台";
        }
    }

    public static Timestamp getNowTimeStamp() {
        /**
         * 取得当前时间戳
         *
         * @return TimStamp
         */
        return new Timestamp(System.currentTimeMillis());
    }

}
